﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MallInteractiveMap.CMSWebClient.Common;
using MallInteractiveMap.CMSWebClient.Presenter;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;

namespace MallInteractiveMap.CMSWebClient.BasicView
{
    public partial class Shop : BasePage
    {
        private ShopPresenter _presenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new ShopPresenter();
            if (!IsPostBack)
            {
                PrepareView();
            }
        }

        protected void rptPhoto_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ImageButton imgRemove = e.Item.FindControl("imgRemove") as ImageButton;
                imgRemove.Click += new ImageClickEventHandler(imgRemove_Click);
            }
        }

        void imgRemove_Click(object sender, ImageClickEventArgs e)
        {
            int imgId = Convert.ToInt32(((ImageButton) sender).CommandArgument);
            if(_presenter.DeletePhoto(imgId))
            {
                PopulatePhoto();
            }
        }

        public int Id
        {
            get { return Request.QueryString["Id"] != null ? Convert.ToInt32(Request.QueryString["Id"]) : 0; }
        }

        private void PrepareView()
        {
            PopulateCategory();
            PopulateFloor();
            if (Id > 0)
            {
                DTOShop cagegory = _presenter.GetShops(Id).FirstOrDefault();
                PopulateControls(cagegory);
            }
            PopulatePhoto();

        }
        private void PopulatePhoto()
        {
            //int imgCount = 0;
            if (Id > 0)
            {
                List<DTOShopPhotos> photos = _presenter.GetShopPhotos(Id);
                if (photos.Count <= 0)
                {
                    rptPhoto.Visible = false;
                }
                else
                {
                    rptPhoto.DataSource = photos;
                    rptPhoto.DataBind();
                }
                //imgCount = photos.Count;
            }
            //if (imgCount < 4)
            //{
            //    fuPhotos.Attributes.Add("maxlength", (4 - imgCount).ToString());
            //    pnlPhotoUpload.Visible = true;
            //}
            //else
            //{
            //    //fuPhotos.Style.Add("visibility", "collapse");
            //    pnlPhotoUpload.Visible = false;
            //}
        }

        private void PopulateCategory()
        {
            List<DTOCategory> categoryList = new CategoryPresenter().GetCategorys();
            Category.DataTextField = "Name";
            Category.DataValueField = "Id";
            Category.DataSource = categoryList;
            Category.DataBind();
        }
        private void PopulateFloor()
        {
            List<DTOFloor> floorList = new FloorPresenter().GetFloors();
            Floor.DataTextField = "Name";
            Floor.DataValueField = "Id";
            Floor.DataSource = floorList;
            Floor.DataBind();
        }

        private void PopulateControls(DTOShop shop)
        {
            ShopTitle.Text = shop.Title;
            Category.SelectedValue = shop.CategoryId.ToString();
            Floor.SelectedValue = shop.FloorId.ToString();
            ShopNo.Text = shop.ShopNo;
            Location.Text = shop.Location;
            Telephone.Text = shop.Phone;
            Fax.Text = shop.Fax;
            Email.Text = shop.Email;
            ContactPerson.Text = shop.ContactPerson;
            Description.Text = shop.Description;


        }
        private DTOShop PopulateObject()
        {
            DTOShop shop = new DTOShop();
            shop.Title = ShopTitle.Text;
            shop.CategoryId = Convert.ToInt32(Category.SelectedValue);
            shop.FloorId = Convert.ToInt32(Floor.SelectedValue);
            shop.ShopNo = ShopNo.Text;
            shop.Location = Location.Text;
            shop.Phone = Telephone.Text;
            shop.Fax = Fax.Text;
            shop.Email = Email.Text;
            shop.ContactPerson = ContactPerson.Text;
            shop.Description = Description.Text;
            shop.LastUpdated = DateTime.Now;
            shop.IsNew = Id <= 0;
            shop.Id = Id;

            #region Populate Photos object
            try
            {
                shop.Photos = new List<DTOShopPhotos>();
                // Get the HttpFileCollection
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.InputStream != null && hpf.ContentLength > 0)
                    {
                        DTOShopPhotos photo = new DTOShopPhotos();
                        photo.FileName = GetFileName(hpf.FileName);
                        photo.FileType = hpf.ContentType;
                        int len = hpf.ContentLength;
                        byte[] img = new byte[len];
                        hpf.InputStream.Read(img, 0, len);
                        photo.ShopImage = img;
                        photo.LastUpdated = DateTime.Now;
                        shop.Photos.Add(photo);
                    }
                   
                }
            }
            catch (Exception ex)
            {

            }
            #endregion
            return shop;
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            DTOShop Shop = PopulateObject();
            if (_presenter.SetData(Shop))
            {
                Response.Redirect("~/BasicView/ShopDetail.aspx");
            }
            else
            {
                ShowMessage(LanguageLoader.GetText("FAILED_OPERATION_MESSAGE"), LanguageLoader.GetText("FAILED"));
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ShopTitleLable.Text = LanguageLoader.GetText("TITLE");
            CategoryLabel.Text = LanguageLoader.GetText("CATEGORY");
            FloorLabel.Text = LanguageLoader.GetText("FLOOR");
            ShopNoLable.Text = LanguageLoader.GetText("SHOP_NUMBER");
            LocationLabel.Text = LanguageLoader.GetText("LOCATION");
            TelephoneLable.Text = LanguageLoader.GetText("TELEPHONE");
            EmailLabel.Text = LanguageLoader.GetText("EMAIL");
            FaxLabel.Text = LanguageLoader.GetText("FAX");
            ContactPersonLabel  .Text = LanguageLoader.GetText("CONTACT_PERSON");
            DescriptionLable.Text = LanguageLoader.GetText("DESCRIPTION");
            SaveShop.Text = LanguageLoader.GetText("SAVE");
            lnkCancel.Text = LanguageLoader.GetText("CANCEL");
        }
    }
}